package cn.com.open.itembank.controller;

import cn.com.open.itembank.common.GlobalConst;
import cn.com.open.itembank.common.enums.Source;
import cn.com.open.itembank.common.enums.SourceType;
import cn.com.open.itembank.dto.*;
import cn.com.open.itembank.interceptor.Log;
import cn.com.open.itembank.service.ItemBankQuestionRemoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by lucky on 2017/7/13.
 */

/**
 * 试题控制器
 */
@RestController
@RequestMapping("/itembank")
public class QuestionController extends BaseController {
    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(QuestionController.class);

    @Autowired
    private ItemBankQuestionRemoteService service;

    //region 新增相关（Create：理论方法 HttpPost）

    //region 新增试题

    /**
     * 新增试题
     *
     * @param request       the request
     * @param response      the response
     * @param itembankId    题库id
     * @param questionModel 试题录入模型
     * @return the return model
     */
    @Log(optType = "新增试题", optDescription = "新增试题，参数：%s", keyName = "questionModel")
    @RequestMapping(value = "/{itembankId}/question", method = RequestMethod.POST)
    public void addQuestion(HttpServletRequest request, HttpServletResponse response,
                            @PathVariable(value = "itembankId") String itembankId,
                            @RequestBody QuestionModel questionModel) {

        String appkey = request.getHeader(appKeyString);
        ReturnModel model = service.addQuestion(itembankId, questionModel, appkey, Source.OUTER.getIndex());

        if (model.getCode() == GlobalConst.COURSE_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 新增试题(内部)

    /**
     * 新增试题（内部）
     *
     * @param request       the request
     * @param response      the response
     * @param itembankId    题库id
     * @param questionModel 试题录入模型
     * @return the return model
     */
    @Log(optType = "新增试题（内部）", optDescription = "新增试题（内部），参数：%s", keyName = "questionModel")
    @RequestMapping(value = "/{itembankId}/question/inneradd", method = RequestMethod.POST)
    public void addQuestionInner(HttpServletRequest request, HttpServletResponse response,
                                 @PathVariable(value = "itembankId") String itembankId,
                                 @RequestBody QuestionModel questionModel) {

        String appkey = request.getHeader(appKeyString);
        ReturnModel model = service.addQuestion(itembankId, questionModel, appkey, Source.INNER.getIndex());

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 导入试题

    /**
     * 导入试题
     *
     * @param itembankId  题库id
     * @param importModel importModel
     * @return the return model
     */
    @Log(optType = "导入试题", optDescription = "导入试题，资源ID：%s", keyName = "resourceId")
    @RequestMapping(value = "/{itembankId}/question/import", method = RequestMethod.POST)
    public void importQuestion(HttpServletRequest request, HttpServletResponse response,
                               @PathVariable(value = "itembankId") String itembankId,
                               @RequestBody ImportParameter importModel) {

        String appkey = request.getHeader(appKeyString);
        importModel.setAppKey(appkey);
        importModel.setAppSecret(request.getHeader(appSecretString));
        importModel.setItemBankId(itembankId);
        importModel.setSourceType(SourceType.QUESTION);
        // 根据appkey的具体值判断内部或外部
        importModel.setSource(appkey == "c655762fdf5979cb382c94061d6ddebb" ? 0 : 1);

        ReturnModel model = null;
        if(StringUtils.isEmpty(importModel.getTextContent())){
            model = service.importQuestion(itembankId, importModel);
        }else{
            model = service.importQuestionByText(itembankId, importModel);
        }

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //endregion

    //region 删除相关（Delete：理论方法 HttpDelete）

    //region 临时删除试题

    /**
     * 临时删除试题
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param questionId 试题id
     * @return the return model
     */
    @Log(optType = "临时删除试题", optDescription = "临时删除试题，ID：%s", keyName = "questionid")
    @DeleteMapping(value = "/{itembankId}/question/{questionid}")
    public void deleteQuestion(HttpServletRequest request, HttpServletResponse response,
                               @PathVariable(value = "itembankId") String itembankId,
                               @PathVariable(value = "questionid") String questionId) {

        String appKey = request.getHeader(appKeyString);
        ReturnModel model = service.deleteQuestion(itembankId, questionId, appKey);

        if (model.getCode() == GlobalConst.SUCCESS) {
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        } else if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    // region 批量临时删除试题

    /**
     * 批量临时删除试题
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param idList     the id list
     * @return the return model
     */
    @Log(optType = "批量临时删除试题", optDescription = "批量临时删除试题，IDs：%s", keyName = "ids")
    @PostMapping(value = "/{itembankId}/question/batchdelete")
    public void deleteQuestionList(HttpServletRequest request, HttpServletResponse response,
                                   @PathVariable(value = "itembankId") String itembankId,
                                   @RequestBody IdentifierArray idList) {

        String appkey = request.getHeader(appKeyString);
        ReturnModel model = service.deleteQuestionList(itembankId, idList, appkey);
        if (model.getCode() == GlobalConst.SUCCESS) {
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        } else if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        }
        responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
    }

    //endregion

    //region 彻底删除试题

    /**
     * 彻底删除试题
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param questionId the question id
     */
    @Log(optType = "彻底删除试题", optDescription = "彻底删除试题，ID：%s", keyName = "questionid")
    @DeleteMapping(value = "/{itembankId}/question/{questionid}/remove")
    public void removeQuestion(HttpServletRequest request, HttpServletResponse response,
                               @PathVariable(value = "itembankId") String itembankId,
                               @PathVariable(value = "questionid") String questionId) {

        String appKey = request.getHeader(appKeyString);
        ReturnModel model = service.removeQuestion(itembankId, questionId, appKey);

        if (model.getCode() == GlobalConst.SUCCESS) {
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        } else if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 批量彻底删除试题

    /**
     * 批量彻底删除试题
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param idList     the id list
     */
    @Log(optType = "批量彻底删除试题", optDescription = "批量彻底删除试题，IDs：%s", keyName = "idList")
    @PostMapping(value = "/{itembankId}/question/batchremove")
    public void removeQuestionList(HttpServletRequest request, HttpServletResponse response,
                                   @PathVariable(value = "itembankId") String itembankId,
                                   @RequestBody IdentifierArray idList) {

        String appkey = request.getHeader(appKeyString);
        ReturnModel model = service.removeQuestionList(itembankId, idList, appkey);

        if (model.getCode() == GlobalConst.SUCCESS) {
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        } else if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 恢复试题

    /**
     * 恢复试题
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param questionId 试题id
     * @return the return model
     */
    @Log(optType = "恢复试题", optDescription = "恢复试题，ID：%s", keyName = "questionId")
    @PutMapping(value = "/{itembankId}/question/{questionId}/recover")
    public void recover(HttpServletRequest request, HttpServletResponse response,
                        @PathVariable(value = "itembankId") String itembankId,
                        @PathVariable(value = "questionId") String questionId) {

        String appkey = request.getHeader(appKeyString);
        ReturnModel model = service.recover(itembankId, questionId, appkey);
        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 批量恢复试题

    /**
     * 批量恢复试题
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param idList     the id list
     */
    @Log(optType = "批量恢复试题", optDescription = "批量恢复试题，IDs：%s", keyName = "idList")
    @PostMapping(value = "/{itembankId}/question/batchrecover")
    public void recoverList(HttpServletRequest request, HttpServletResponse response,
                            @PathVariable(value = "itembankId") String itembankId,
                            @RequestParam("ids") String idList) {

        String appkey = request.getHeader(appKeyString);
        ReturnModel model = service.recoverList(itembankId, idList, appkey);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //endregion

    //region 更新相关（Update：理论方法 HttpPut）

    //region 修改试题审核状态

    /**
     * 修改试题审核状态
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param questionId 试题id
     * @param isCheck    True，审核通过；False，取消审核
     */
    @Log(optType = "修改试题审核状态", optDescription = "修改试题审核状态，ID：%s", keyName = "questionId")
    @RequestMapping(value = "/{itembankId}/question/{questionId}/check", method = RequestMethod.PUT)
    public void updateCheckStatus(HttpServletRequest request, HttpServletResponse response,
                                  @PathVariable(value = "itembankId") String itembankId,
                                  @PathVariable(value = "questionId") String questionId,
                                  @RequestParam("isCheck") boolean isCheck) {

        String appkey = request.getHeader(appKeyString);
        ReturnModel model = service.updateCheckStatus(itembankId, questionId, appkey, isCheck);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 修改试题停启用状态

    /**
     * 修改试题停启用状态
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param questionId 试题id
     */
    @Log(optType = "修改试题停启用状态", optDescription = "修改试题停启用状态，ID：%s", keyName = "questionId")
    @RequestMapping(value = "/{itembankId}/question/{questionId}/status", method = RequestMethod.PUT)
    public void updateStatus(HttpServletRequest request, HttpServletResponse response,
                             @PathVariable(value = "itembankId") String itembankId,
                             @PathVariable(value = "questionId") String questionId) {

        String appkey = request.getHeader(appKeyString);
        ReturnModel model = service.updateStatus(itembankId, questionId, appkey);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 批量修改试题停启用状态

    /**
     * 批量修改试题停启用状态
     *
     * @param request     the request
     * @param response    the response
     * @param itembankId  题库id
     * @param statusModel 更新状态对象
     * @return the return model
     */
    @Log(optType = "批量修改试题停启用状态", optDescription = "批量修改试题停启用状态，IDs：%s", keyName = "idList")
    @PutMapping(value = "/{itembankId}/question/status")
    public void batchUpdateStatus(HttpServletRequest request, HttpServletResponse response,
                                  @PathVariable(value = "itembankId") String itembankId,
                                  @RequestBody UpdateQuestionStatus statusModel) {

        String appkey = request.getHeader(appKeyString);
        ReturnModel model = service.batchUpdateStatus(itembankId, statusModel, appkey);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregions

    //region 修改试题

    /**
     * 修改试题
     *
     * @param request       the request
     * @param response      the response
     * @param itembankId    题库ID
     * @param questionid    试题id
     * @param questionModel 试题模型
     */
    @Log(optType = "修改试题", optDescription = "修改试题，ID：%s", keyName = "questionid")
    @PutMapping(value = "/{itembankId}/question/{questionid}")
    public void updateQuestion(HttpServletRequest request, HttpServletResponse response,
                               @PathVariable(value = "itembankId") String itembankId,
                               @PathVariable(value = "questionid") String questionid,
                               @RequestBody QuestionModel questionModel) {

        questionModel.setI4(itembankId);
        String appkey = request.getHeader(appKeyString);
        ReturnModel model = service.updateQuestion(itembankId, questionid, questionModel, appkey);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //endregion

    //region 查询相关（Retrieve：理论方法 HttpGet）

    //region 获取试题模型

    /**
     * 获取试题模型
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @return the return
     */
    @Log(optType = "获取试题模型", optDescription = "获取试题模型", keyName = "itembankId")
    @GetMapping(value = "/{itembankId}/question")
    public void getQuestionModel(HttpServletRequest request, HttpServletResponse response,
                                 @PathVariable(value = "itembankId") String itembankId) {

        if (!isPermissionForItemBank(request, itembankId)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        QuestionModel model1 = new QuestionModel();
        ReturnModel model = new ReturnModel();
        model.setData(model1);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 获取试题列表

    /**
     * 获取试题列表
     * 历史接口：/openapi/v3/itembank/{itembankId}/question/GetList
     *
     * @param request        the request
     * @param response       the response
     * @param itembankId     题库id
     * @param filterModelDto the filter model dto
     * @return the return
     */
    @Log(optType = "获取试题列表", optDescription = "获取试题列表，参数：%s", keyName = "filterModelDto")
    @PostMapping(value = "/{itembankId}/question/getlist")
    public void getList(HttpServletRequest request, HttpServletResponse response,
                        @PathVariable(value = "itembankId") String itembankId,
                        @RequestBody FilterModelDto filterModelDto) {

        if (!isPermissionForItemBank(request, itembankId)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        ReturnModel model = service.getList(itembankId, filterModelDto, true, false);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 获取试题列表（内部）

    /**
     * 获取试题列表（内部）
     * 历史接口：/openapi/v3/itembank/{itembankId}/question/GetInnerList
     *
     * @param request        the request
     * @param response       the response
     * @param itembankId     题库id
     * @param filterModelDto the filter model dto
     * @return the return
     */
    @Log(optType = "获取试题列表(内部)", optDescription = "获取试题列表(内部)，参数：%s", keyName = "filterModelDto")
    @PostMapping(value = "/{itembankId}/question/getinnerlist")
    public void getInnerList(HttpServletRequest request, HttpServletResponse response,
                             @PathVariable(value = "itembankId") String itembankId,
                             @RequestBody FilterModelDto filterModelDto) {

        if (!isPermissionForItemBank(request, itembankId)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        ReturnModel model = service.getList(itembankId, filterModelDto, false, false);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 获取试题列表(一级机考)

    /**
     * 获取试题列表(一级机考)
     *
     * @param request        the request
     * @param response       the response
     * @param itembankId     题库id
     * @param filterModelDto the filter model dto
     * @return the return
     */
    @Log(optType = "获取试题列表(一级机考)", optDescription = "获取试题列表(一级机考)，参数：%s", keyName = "filterModelDto")
    @PostMapping(value = "/{itembankId}/question/getquestionlistforonlineexam")
    public void getQuestionListForOnlineExam(HttpServletRequest request, HttpServletResponse response,
                                             @PathVariable(value = "itembankId") String itembankId,
                                             @RequestBody FilterModelDto filterModelDto) {

        if (!isPermissionForItemBank(request, itembankId)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        ReturnModel model = service.getList(itembankId, filterModelDto, true, true);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 获取试题列表

    /**
     * 获取试题列表(入学测试机考)
     *
     * @param request        the request
     * @param response       the response
     * @param itembankId     题库id
     * @param filterModelDto the filter model dto
     * @return the return
     */
    @Log(optType = "获取试题列表(入学测试机考)", optDescription = "获取试题列表(入学测试机考)，参数：%s", keyName = "filterModelDto")
    @PostMapping(value = "/{itembankId}/question/getquestionlist")
    public void getQuestionList(HttpServletRequest request, HttpServletResponse response,
                                @PathVariable(value = "itembankId") String itembankId,
                                @RequestBody FilterModelDto filterModelDto) {

        if (!isPermissionForItemBank(request, itembankId)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        ReturnModel model = service.getList(itembankId, filterModelDto, true, true);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 获取试题回收站列表

    /**
     * 获取试题回收站列表
     *
     * @param request        the request
     * @param response       the response
     * @param itembankId     题库id
     * @param filterModelDto 数据集合
     * @return the return model
     */
    @Log(optType = "获取试题回收站列表", optDescription = "获取试题回收站列表，参数：%s", keyName = "filterModelDto")
    @PostMapping(value = "/{itembankId}/question/recylebin", consumes = "application/json")
    public void getRycleBinList(HttpServletRequest request, HttpServletResponse response,
                                @PathVariable(value = "itembankId") String itembankId,
                                @RequestBody FilterModelDto filterModelDto) {

        if (!isPermissionForItemBank(request, itembankId)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        ReturnModel model = service.getRycleBinList(itembankId, filterModelDto);
        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 导出试题

    /**
     * 导出试题
     *
     * @param request        the request
     * @param response       the response
     * @param itembankId     题库id
     * @param filterModelDto 试题查询筛选模型
     * @return the return
     */
    @Log(optType = "导出试题", optDescription = "导出试题，参数：%s", keyName = "filterModelDto")
    @PostMapping(value = "/{itembankId}/question/exportquestion")
    public void exportQuestion(HttpServletRequest request, HttpServletResponse response,
                               @PathVariable(value = "itembankId") String itembankId,
                               @RequestBody FilterModelDto filterModelDto) {

        if (!isPermissionForItemBank(request, itembankId)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        ReturnModel model = service.exportQuestion(itembankId, filterModelDto);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else if (model.getCode() == GlobalConst.NO_PERMISSION) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 获取试题完整信息

    /**
     * 获取试题完整信息
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param questionId 试题id
     * @return 试题模型
     */
    @Log(optType = "获取试题完整信息", optDescription = "获取试题完整信息，ID：%s", keyName = "questionId")
    @RequestMapping(value = "/{itembankId}/question/{questionId}", method = RequestMethod.GET)
    public void getFullItem(HttpServletRequest request, HttpServletResponse response,
                            @PathVariable(value = "itembankId") String itembankId,
                            @PathVariable(value = "questionId") String questionId) {

        if (!isPermissionForItemBank(request, itembankId)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        ReturnModel model = service.getFullItem(itembankId, questionId);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 获取试题含答案信息

    /**
     * 获取试题含答案信息
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param questionId 试题id
     * @return 试题答案模型
     */
    @Log(optType = "获取试题含答案信息", optDescription = "获取试题含答案信息，ID：%s", keyName = "questionId")
    @RequestMapping(value = "/{itembankId}/question/{questionId}/answer", method = RequestMethod.GET)
    public void getItemAnswer(HttpServletRequest request, HttpServletResponse response,
                              @PathVariable(value = "itembankId") String itembankId,
                              @PathVariable(value = "questionId") String questionId) {

        if (!isPermissionForItemBank(request, itembankId)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        ReturnModel model = service.getItemAnswer(itembankId, questionId);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 获取试题不含答案信息

    //region 获取试题不含答案信息

    /**
     * 获取指定题库下指定试题不含答案的信息
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param questionId 试题id
     * @return 试题答案模型
     */
    @Log(optType = "获取指定题库下指定试题不含答案的信息", optDescription = "获取指定题库下指定试题不含答案的信息，ID：%s", keyName = "questionId")
    @GetMapping(value = "/{itembankId}/question/{questionId}/withoutanswer")
    public void getItemWithoutAnswer(HttpServletRequest request, HttpServletResponse response,
                                     @PathVariable(value = "itembankId") String itembankId,
                                     @PathVariable(value = "questionId") String questionId) {

        if (!isPermissionForItemBank(request, itembankId)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        ReturnModel model = service.getItemWithoutAnswer(itembankId, questionId);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 根据题库id获取指定题库下试题按照题型，难度，应用类别的数量统计列表

    /**
     * 根据题库id获取指定题库下试题按照题型，难度，应用类别的数量统计列表.
     * 历史接口：openapi/v3/ItemBank/{itembankId}/GetQuestionCount
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     */
    @Log(optType = "根据题库id获取指定题库下试题按照题型，难度，应用类别的数量统计列表", optDescription = "根据题库id获取指定题库下试题按照题型，难度，应用类别的数量统计列表 %s", keyName = "itembankId")
    @RequestMapping(value = "/{itembankId}/question/stat/oes", method = RequestMethod.GET)
    public void getQuestionStatByItemBankId(HttpServletRequest request, HttpServletResponse response,
                                            @PathVariable(value = "itembankId") String itembankId) {

        if (!isPermissionForItemBank(request, itembankId)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        ReturnModel model = service.getQuestionStatByItemBankId(itembankId);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 随机获取指定题库指定题型指定数量的试题集合

    /**
     * 随机获取指定题库指定题型指定数量的试题集合.
     * 历史接口：openapi/v3/ItemBank/{cid}/type/{tid}/GetRandomQuestions
     *
     * @param request        the request
     * @param response       the response
     * @param itembankId     题库id
     * @param questionTypeId 题型id
     * @param cnt            获取数量
     */
    @Log(optType = "随机获取指定题库指定题型指定数量的试题集合", optDescription = "随机获取指定题库指定题型指定数量的试题集合 %s", keyName = "itembankId")
    @RequestMapping(value = "/{itembankId}/questiontype/{questionTypeId}/question/random", method = RequestMethod.GET)
    public void getRandomQuestionsByItemBankIdAndQType(HttpServletRequest request, HttpServletResponse response,
                                                       @PathVariable(value = "itembankId") String itembankId,
                                                       @PathVariable(value = "questionTypeId") String questionTypeId,
                                                       @RequestParam(value = "cnt") int cnt) {

        if (!isPermissionForItemBank(request, itembankId)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        ReturnModel model = service.getRandomQuestionsByItemBankIdAndQType(itembankId, questionTypeId, cnt);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 根据题库id获取指定题库下试题按照应用类别的数量统计列表

    /**
     * 根据题库id获取指定题库下试题按照应用类别的数量统计列表.
     * 历史接口：/openapi/v3/Course/{courseId}/Question/GetQuestionCountByQuestionType
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     */
    @Log(optType = "根据题库id获取指定题库下试题按照应用类别的数量统计列表", optDescription = "根据题库ID %s获取指定题库下试题按照应用类别的数量统计列表", keyName = "itembankId")
    @RequestMapping(value = "/{itembankId}/question/category/stat", method = RequestMethod.GET)
    public void getQuestionStatForCategoryByItemBankId(HttpServletRequest request, HttpServletResponse response,
                                                       @PathVariable(value = "itembankId") String itembankId) {

        if (!isPermissionForItemBank(request, itembankId)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        ReturnModel model = service.getQuestionStatForCategoryByItemBankId(itembankId);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //region 根据题库id获取指定题库下指定题型的试题按照给定的章节,应用类别统计的难中易三档题数列

    /**
     * 根据题库id获取指定题库下指定题型的试题按照给定的章节,应用类别统计的难中易三档题数列表.
     * 历史接口：/openapi/v3/ItemBank/{ItemBankId}/Question/ConditionCount
     * 撤销历史接口：/openapi/v3/Course/{CourseId}/Question/ConditionCountByCourseId
     *
     * @param request    the request
     * @param response   the response
     * @param itembankId 题库id
     * @param cntModel   the cnt model
     */
    @Log(optType = "根据题库id获取指定题库下指定题型的试题按照给定的章节,应用类别统计的难中易三档题数列表", optDescription = "根据题库ID %s获取指定题库下指定题型的试题按照给定的章节,应用类别统计的难中易三档题数列表", keyName = "itembankId")
    @RequestMapping(value = "/{itembankId}/question/difficulty/stat", method = RequestMethod.POST)
    public void getQuestionDifficultyStatByItemBankId(HttpServletRequest request, HttpServletResponse response,
                                                      @PathVariable(value = "itembankId") String itembankId,
                                                      @RequestBody DifficultyCountModel cntModel) {

        if (!isPermissionForItemBank(request, itembankId)) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }
        ReturnModel model = service.getQuestionDifficultyStatByItemBankId(itembankId, cntModel);

        if (model.getCode() == GlobalConst.ID_NOT_EXIST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else {
            responseJson(request, response, model.getData(), model.getMessage(), model.getCode());
        }
    }

    //endregion

    //endregion
}
